package com.qzct.controller;

import com.qzct.entity.Banner;
import com.qzct.entity.vo.ResultCode;
import com.qzct.entity.vo.ResultVo;
import com.qzct.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "文件上传测试")
@RequestMapping("/file")
public class FileUploadController {

    @Autowired
    FileService fileService;
    @PostMapping(value = "/upload/image",consumes = "multipart/form-data")
    @ApiOperation("图片上传测试,返回访问地址")
    public ResultVo imageUploader(@RequestPart(value = "image") MultipartFile image){
        String fileName=image.getOriginalFilename();
        String filepath = fileService.upLoadImg("test",fileName,image);
        if(filepath==null){
            return ResultVo.error(ResultCode.FILE_UPLOAD_ERROR);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("data",filepath);
        return  new ResultVo(filepath);
    }

    @PostMapping(value = "/upload/images",consumes = "multipart/form-data")
    @ApiOperation("批量上传")
    public ResultVo imageUploader(@RequestPart(value = "图片") MultipartFile [] images, int id){
        Map<String,Object> map=new HashMap<>();
        ArrayList<String> list = new ArrayList<>();
        String filepath = null;
        for (int i = 0; i < images.length; i++) {
            filepath = "";
            System.out.println(images[i].getOriginalFilename());
            String fileName=images[i].getOriginalFilename();
            filepath = fileService.upLoadImg("test",fileName,images[i]);
            if(filepath==null){
                return ResultVo.error(ResultCode.FILE_UPLOAD_ERROR);
            }
            list.add(filepath);
        }
        map.put("id",id);
        map.put("data",list);
        return  new ResultVo(map);
//        String fileName=image.getOriginalFilename();
//        String filepath = fileService.upLoadImg("test",fileName,image);
//        if(filepath==null){
//            return ResultVo.error(ResultCode.FILE_UPLOAD_ERROR);
//        }
//        Map<String,Object> map=new HashMap<>();
//        map.put("data",filepath);
//        return  new ResultVo(filepath);

    }

}
